#include "FKSimpleDrawModel.h"

#include "FKLogger.h"
#include "FKSDM.h"

#include <QVector3D>

FKSimpleDrawModel::FKSimpleDrawModel(QString modelType, float width, QString modelName, QObject* parent):
        FKDrawModel(parent),_x(width),_y(0),_z(0),_modelType(modelType){
    FK_CBEGIN
    loadModel(modelName,QString());
    FK_CEND
}

FKSimpleDrawModel::FKSimpleDrawModel(QString modelType, float width, float height, QString modelName, QObject* parent):
        FKDrawModel(parent),_x(width),_y(height),_z(0),_modelType(modelType){
    FK_CBEGIN
    loadModel(modelName,QString());
    FK_CEND
}

FKSimpleDrawModel::FKSimpleDrawModel(QString modelType, float width, float height, float depth, QString modelName, QObject* parent):
        FKDrawModel(parent),_x(width),_y(height),_z(-depth),_modelType(modelType){
    FK_CBEGIN
    loadModel(modelName,QString());
    FK_CEND
}

FKSimpleDrawModel::~FKSimpleDrawModel(){
    FK_DBEGIN
    FK_DEND
}

bool FKSimpleDrawModel::modelLoading(QString modelName, QString path){
    Q_UNUSED(path)
    Q_UNUSED(modelName)
    if(_modelType==FKSDM::quad){
        if(_x!=0){
            if(_y==0)_y=_x;
            if(_z){
                loadCube();
            }else{
                loadQuad();
            }
            _verticeCount=_vertices.size();
            _indexCount=_indexes.size();
            return true;
        }
    }
    return false;
}

void FKSimpleDrawModel::loadQuad(){
    float x=_x/2.0;
    float y=_y/2.0;
    float z=_z/2.0;
    _vertices<<QVector3D(-x,-y,-z)<<QVector3D(x,-y,-z)<<QVector3D(x,y,-z)<<QVector3D(-x,y,-z);
    _indexes<<0<<1<<2<<0<<2<<3;
    _colors<<QVector3D(0.4f,0.4f,0.4f)<<QVector3D(0.4f,0.4f,0.4f)<<QVector3D(1.0f,1.0f,1.0f)<<QVector3D(1.0f,1.0f,1.0f)
             <<QVector3D(0.0f,0.0f,0.0f)<<QVector3D(0.0f,0.0f,0.0f)<<QVector3D(0.9f,0.9f,0.9f)<<QVector3D(0.9f,0.9f,0.9f);
}

void FKSimpleDrawModel::loadCube(){
    loadQuad();
    float x=_x/2.0;
    float y=_y/2.0;
    float z=_z/2.0;
    _vertices<<QVector3D(-x,-y,z)<<QVector3D(x,-y,z)<<QVector3D(x,y,z)<<QVector3D(-x,y,z);
    _indexes<<4<<0<<3<<4<<3<<7 //left
            <<1<<5<<6<<1<<6<<2 //right
            <<5<<4<<7<<5<<7<<6 //rear
            <<3<<2<<6<<3<<6<<7 //top
            <<4<<5<<1<<4<<1<<0;//bottom

//      _indexes<<0<<1<<2<<0<<2<<3;
//    loadQuad();
}
